# 添加商品页面定位
import time

from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select

from base.admin.admin_base_page import AdminBasePage


# 对象层+操作层+业务层
class CatePage(AdminBasePage):
    def __init__(self):
        #
        super().__init__()
        # iframe
        self.freame_ele = (By.ID, "workspace")
        # 新增分类按钮
        self.shop_name = (By.CSS_SELECTOR, '[title="新增分类"]')
        # 分类名称
        self.cate_name = (By.NAME, "name")
        # 手机分类名称
        self.phone_name = (By.NAME, "mobile_name")
        # 一级分类名称
        self.one_cate = (By.ID, "parent_id_1")
        # 二级分类名称
        self.two_cate = (By.ID, "parent_id_2")
        # 导航显示
        self.nav_cate = (By.CLASS_NAME, ".cb-enable ")
        # 排序
        self.order_cate = (By.ID, "t_sort")
        # 分佣比例
        self.oper = (By.NAME, "commission_rate")
        # 点击提交
        self.sub_bnt = (By.ID, "submitBtn")

    # 新增分类
    def add_cate(self, cate_name, phone_name, o_catatext, order_num_text, t_catatext, oper_text):
        """
        :param shop_name: 商品分类栏输入名称
        :param phone_name: 手机分类栏输入名称
        :param o_catatext: 一级分类选项框选择商品归属一级类
        :param t_catatext: 二级分类选项框选择商品归属二级类
        :param order_num_text: 排序权重值
        :param oper_text: 佣金比例值
        :return:
        """
        # 切换iframe（为什么要切换到iframe)
        self.driver.switch_to.frame(self.find_elem(self.freame_ele))
        # 点击添加商品按钮
        time.sleep(2)
        self.find_elem(self.shop_name).click()
        time.sleep(6)
        # 输入分类名称
        self.input_elem(elem=self.cate_name, text=cate_name)
        # 输入手机分类名称
        self.input_elem(elem=self.phone_name, text=phone_name)
        # 选择一级分类
        self.find_elem(self.one_cate).click()
        Select(self.find_elem(self.one_cate)).select_by_visible_text(o_catatext)
        # 选择二级分类
        self.find_elem(self.two_cate).click()
        time.sleep(2)
        Select(self.find_elem(self.two_cate)).select_by_visible_text(t_catatext)
        # 导航显示点击是
        self.find_elem(self.nav_cate).click()
        # 排序输入框
        self.input_elem(self.order_cate, order_num_text)
        # 分佣比例输入框
        self.input_elem(self.oper, oper_text)
        # 确认提交按钮点击
        self.find_elem(self.sub_bnt).click()
